package com.intellagric.controller.product;

import com.intellagric.common.bean.LayuiDataGridResult;
import com.intellagric.common.bean.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.interfaces.product.ProductService;
import com.intellagric.pojo.Product;
import com.intellagric.pojo.ProductUsed;
import com.intellagric.pojo.SysOperate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/admin")
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("/product/add")
    @ResponseBody
    public ResponseMessage addProduct(Product product){
        product.setProduceId(UUIDUtils.getID());
        product.setCreateDate(new Date(System.currentTimeMillis()));
        return productService.insertProduct(product);
    }

    @RequestMapping("/product/edit")
    @ResponseBody
    public ResponseMessage editProduct(Product product){
        return productService.editProduct(product);
    }

    @RequestMapping("/product/delete")
    @ResponseBody
    public ResponseMessage deleteProduct(String id){
        return productService.deleteProduct(id);
    }

    @RequestMapping("/product/batchdelete")
    @ResponseBody
    public LayuiDataGridResult batchDelete(@RequestParam("ids[]") String[] ids){
        return productService.batchDeleteProduct(ids);
    }

    @RequestMapping("/product/getProduct")
    @ResponseBody
    public Product getProduct(String productId){
        return productService.queryProductById(productId);
    }

    @RequestMapping("/product/getProductList")
    @ResponseBody
    public LayuiDataGridResult getProduceList(int page, int limit){
        return productService.getList(page,limit);
    }

    @RequestMapping("/product/getProductListByCate")
    @ResponseBody
    public LayuiDataGridResult getProduceListByCate(int page, int limit, String categoryId){
        return productService.getListByCategoryId(page,limit,categoryId);
    }
    @RequestMapping("/product/getProductListByCateNotLay")
    @ResponseBody
    public List<Product> getProduceListByCateNotLay(int page, int limit, String categoryId){
        return productService.getListByCategoryId(page,limit,categoryId).getData();
    }

    @RequestMapping("/product/search")
    @ResponseBody
    public LayuiDataGridResult search(int page, int limit, String keyword){
        return productService.getProductByKeyword(page,limit,keyword);
    }
    @RequestMapping("/product/getPropCount")
    @ResponseBody
    public List<Map<String,String>> getPropCount(){
        return productService.getPropCount();
    }

    /**
     * 导入耗材excel表格批量添加数据
     * @return
     */
    @RequestMapping(value = "/product/addConsumableExcel")
    @ResponseBody
    public LayuiDataGridResult addConsumableExcelData(MultipartFile uploadFile){
        LayuiDataGridResult result=new LayuiDataGridResult();
        String fileName=uploadFile.getOriginalFilename();
        if (Pattern.matches(".*xls.*",fileName.substring(fileName.lastIndexOf(".")+1))){
            return productService.addConsumableExcelData(uploadFile);
        }
        result.setMsg("文件格式错误");
        result.setCode(100);
        return result;
    }


    /**
     * 导出报表
     * @return
     */
    @RequestMapping("/export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        productService.export(request, response);
    }





}
